package com.example.springboot.indexsearcher;

import com.example.springboot.util.IndexUtil;
//import org.apache.lucene.facet.FacetsConfig;
import org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyWriter;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;

import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.List;
import java.util.concurrent.CountDownLatch;

public abstract class BaseIndex<T> implements Runnable {
    /**
     * 父级索引路径
     */
    private String parentIndexPath;
    /**
     * 索引编写器
     */
    private IndexWriter writer;
    private int subIndex;
    /**
     * 主线程
     */
    private final CountDownLatch countDownLatch1;
    /**
     * 工作线程
     */
    private final CountDownLatch countDownLatch2;
    /**
     * 对象列表
     */
    private List<T> list;
    /**
     * facet查询
     */
    private String facet;
   // protected final FacetsConfig config = new FacetsConfig();
    protected final static String indexPath = "index1";
    protected final static DirectoryTaxonomyWriter taxoWriter;

    static {
        try {
            Directory directory = FSDirectory.open(new File(indexPath));
            taxoWriter = new DirectoryTaxonomyWriter(directory);
        } catch (IOException e) {
            throw new ExceptionInInitializerError("BaseIndex initializing error");
        }
    }

    public BaseIndex(IndexWriter writer, CountDownLatch countDownLatch1, CountDownLatch countDownLatch2,
                     List<T> list, String facet) {
        super();
        this.writer = writer;
        this.countDownLatch1 = countDownLatch1;
        this.countDownLatch2 = countDownLatch2;
        this.list = list;
        this.facet = facet;
    }

    public BaseIndex(String parentIndexPath, int subIndex,
                     CountDownLatch countDownLatch1, CountDownLatch countDownLatch2,
                     List<T> list) {
        super();
        this.parentIndexPath = parentIndexPath;
        this.subIndex = subIndex;
        try {
            //多目录索引创建
            File file = new File(parentIndexPath + "/index" + subIndex);
            if (!file.exists()) {
                file.mkdir();
            }
            this.writer = IndexUtil.getIndexWriter(parentIndexPath + "/index" + subIndex, true);
        } catch (IOException e) {
            e.printStackTrace();
        }
        ;
        this.subIndex = subIndex;
        this.countDownLatch1 = countDownLatch1;
        this.countDownLatch2 = countDownLatch2;
        this.list = list;
    }

    public BaseIndex(String path, CountDownLatch countDownLatch1, CountDownLatch countDownLatch2,
                     List<T> list) {
        super();
        try {
            //单目录索引创建
            File file = new File(path);
            if (!file.exists()) {
                file.mkdir();
            }
            this.writer = IndexUtil.getIndexWriter(path, true);
        } catch (IOException e) {
            e.printStackTrace();
        }
        ;
        this.countDownLatch1 = countDownLatch1;
        this.countDownLatch2 = countDownLatch2;
        this.list = list;
    }


    public abstract void indexDoc(IndexWriter writer, T t) throws Exception;


    public void indexDocs(IndexWriter writer, List<T> t) throws Exception {
        for (T t2 : t) {
            indexDoc(writer, t2);
        }
    }

    @Override
    public void run() {
        try {
            countDownLatch1.await();
            System.out.println(writer);
            indexDocs(writer, list);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            countDownLatch2.countDown();
            try {
                writer.close();
                writer.close();
            } catch (IOException e) {
                e.printStackTrace();
            }


        }
    }
}